﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using blackhouse.Data;

namespace blackhouse.BlackApart.Data.Sql {
	public class NewsletterDataAccess : AdoNewsletterDataAccess {

		#region Fields

		private string connectionString;
		private MsSqlAdoDataHelper dataHelper;

		#endregion

		#region Constructors

		public NewsletterDataAccess(string sqlConnectionString) {
			this.connectionString = sqlConnectionString;
			this.dataHelper = new MsSqlAdoDataHelper(this.connectionString);
			SqlVersioning versioning = new SqlVersioning(this.connectionString, this.GetType().Assembly);
		}

		#endregion

		#region AdoCompanyDataAccess Members

		public override bool CheckEmailExist(string email) {
			this.dataHelper.Reset();
			this.dataHelper.ProcedureName = "bhp_NewsletterCheckEmailExist";
			this.dataHelper.AddParameter("Email", email, System.Data.DbType.String);
			return Convert.ToBoolean(this.dataHelper.ExecuteScalar());
		}

		public override bool SaveSubscriber(string email, Guid uniqueId, string language) {
			this.dataHelper.Reset();
			this.dataHelper.ProcedureName = "bhp_NewsletterSubscriberSave";
			this.dataHelper.AddParameter("Email", email, System.Data.DbType.String);
			this.dataHelper.AddParameter("UniqueId", uniqueId, System.Data.DbType.Guid);
			this.dataHelper.AddParameter("Language", language, System.Data.DbType.StringFixedLength, 5);
			return this.dataHelper.ExecuteWithoutResult() > 0;
		}

		public override string GetSubscriberEmail(Guid uniqueId) {
			this.dataHelper.Reset();
			this.dataHelper.ProcedureName = "bhp_NewsletterSubscriberGet";
			this.dataHelper.AddParameter("UniqueId", uniqueId, System.Data.DbType.Guid);
			return Convert.ToString(this.dataHelper.ExecuteScalar());
		}

		public override bool Confirm(string email) {
			this.dataHelper.Reset();
			this.dataHelper.ProcedureName = "bhp_NewsletterSubscriberConfirm";
			this.dataHelper.AddParameter("Email", email, System.Data.DbType.String);
			return this.dataHelper.ExecuteWithoutResult() > 0;
		}

		public override bool Remove(string email) {
			this.dataHelper.Reset();
			this.dataHelper.ProcedureName = "bhp_NewsletterSubscriberRemove";
			this.dataHelper.AddParameter("Email", email, System.Data.DbType.String);
			return this.dataHelper.ExecuteWithoutResult() > 0;
		}

		#endregion

	}
}
